// lowpassfilter.h

#ifndef LOWPASSFILTER_H
#define LOWPASSFILTER_H

#include "filter.h"
#include <math.h>
#include <visibility.h>

template<typename T>
class __EXPORT LowPassFilter : public Filter<T>
{
public:
	LowPassFilter(float alpha, T maxstep = -1)
		: alpha_((alpha > 0.0f && alpha < 1.0f) ? alpha : 1.0f)
		, initialized_(false)
		, value_(0)
		, maxstep_(maxstep)
	{
	}
	
	virtual ~LowPassFilter() {}

	virtual T update(T input)
	{
		if (initialized_ && (maxstep_ <= 0 || static_cast<T>(fabs(input - value_)) < maxstep_))
			value_ = static_cast<T>(input * alpha_ + value_ * (1.0f - alpha_));
		else
		{
			value_ = input;
			initialized_ = true;
		}

		return value_;
	}
	
	virtual T operator()()
	{
		return value_;
	}

	virtual void set(T value)
	{
		value_ = value;
	}
	
	void setAlpha(float alpha)
	{
		alpha_ = alpha;
	}

private:
	LowPassFilter();

	float alpha_;
	bool initialized_;
	T value_;
	T maxstep_;
};


#endif
